package org.jeecg.modules.car.service.impl;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.util.DateUtils;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.config.Sm4Util;
import org.jeecg.entity.car.CarOrder;
import org.jeecg.modules.car.entity.CarMerchantOrder;
import org.jeecg.modules.car.entity.CarPayNotify;
import org.jeecg.modules.car.mapper.CarMerchantOrderMapper;
import org.jeecg.modules.car.service.ICarMerchantOrderService;
import org.jeecg.modules.car.service.ICarOrderService;
import org.jeecg.modules.system.util.RestTemplateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
@Slf4j
public class SyncCarServiceImpl {

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private CarMerchantOrderMapper carMerchantOrderMapper;

    @Autowired
    private ICarMerchantOrderService carMerchantOrderService;

    @Autowired
    private CarOrderServiceImpl carOrderService;

    /**
     * 商家订单
     * @param transNo
     */
    @Async
    public void queryPayResult(String transNo) {
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        if (redisUtil.get(CommonConstant.LOCK_MERCHANT_ORDER+transNo) != null){
            log.info("商家支付状态查询已有回调,结束查询:{}",transNo);
            return;
        }
        final long timeInterval = 5000;
        int i = 1;
        while (i <= 20){
            Map<String,String> map = new HashMap<>();
            map.put("transNo",transNo);
            String jsonString = JSONObject.toJSONString(map);
            JSONObject jsonObject = null;
            try {
//                jsonObject =  RestTemplateUtil.doPost("https://fronttest.shfft.com/community-front/ori/queryPayResult",jsonString);
                jsonObject =  RestTemplateUtil.doPost("https://sq.shfft.com/community-front/ori/queryPayResult",jsonString);
                log.info("商家支付状态查询返回:{},执行次数:{},订单号:{}", jsonObject,i,transNo);
                if ("02".equals(jsonObject.getString("data"))){
                    log.info("商家支付查询状态为支付成功,订单号:{}",transNo);
                    CarMerchantOrder carMerchantOrder =  carMerchantOrderMapper.getOrderByTransNo(transNo);
                    if (carMerchantOrder !=null){
                        CarPayNotify stu = new CarPayNotify();
                        stu.setTransNo(transNo);
                        stu.setOrderId(carMerchantOrder.getSn());
                        stu.setPaymentAmt(carMerchantOrder.getPayAmount().toString());
                        stu.setPaymentTime(DateUtils.date2Str(carMerchantOrder.getPayTime(),DateUtils.yyyymmddhhmmss));
                        carMerchantOrderService.carPayNotify(stu);
                    }else {
                        log.warn("商家支付状态查询根据transNo未取到记录值");
                    }
                    break;
                } else if ("03".equals(jsonObject.getString("data"))){
                    log.info("商家支付查询状态为支付失败,订单号:{}",transNo);
                    break;
                } else {
                    Thread.sleep(timeInterval);
                }
            } catch (Exception e) {
                log.error("订单支付状态查询异常:执行次数:{},订单号:{}",e,i,transNo);
            }finally {
                i++;
            }
        }
    }


    /**
     * 临停订单
     * @param transNo
     */
    @Async
    public void queryTempPayResult(String transNo) {
        final long timeInterval = 5000;
        int i = 1;
        while (i <= 20){
            Map<String,String> map = new HashMap<>();
            map.put("transNo",transNo);
            String jsonString = JSONObject.toJSONString(map);
            JSONObject jsonObject = null;
            try {
//                jsonObject =  RestTemplateUtil.doPost("https://fronttest.shfft.com/community-front/ori/queryPayResult",jsonString);
                jsonObject =  RestTemplateUtil.doPost("https://sq.shfft.com/community-front/ori/queryPayResult",jsonString);
                log.info("临停订单支付状态查询返回:{},执行次数:{},订单号:{}", jsonObject,i,transNo);
                if ("02".equals(jsonObject.getString("data"))){
                    log.info("临停支付查询状态为支付成功,订单号:{}",transNo);
                    CarOrder carOrder =  carOrderService.getOrderByTransNo(transNo);
                    if (carOrder !=null){
                        CarPayNotify stu = new CarPayNotify();
                        stu.setTransNo(transNo);
                        stu.setOrderId(carOrder.getSn());
                        stu.setPaymentAmt(carOrder.getPayAmount().toString());
                        stu.setPaymentTime(DateUtils.date2Str(carOrder.getPayTime(),DateUtils.yyyymmddhhmmss));
                        carOrderService.carPayNotify(stu);
                    }else {
                        log.warn("临停支付状态查询根据transNo未取到记录值");
                    }
                    break;
                } else if ("03".equals(jsonObject.getString("data"))){
                    log.info("临停支付查询状态为支付失败,订单号:{}",transNo);
                    break;
                } else {
                    Thread.sleep(timeInterval);
                }
            } catch (Exception e) {
                log.error("临停支付状态查询异常:执行次数:{},订单号:{}",e,i,transNo);
            }finally {
                i++;
            }
        }
    }

}
